package heven.holt.wan.android.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import dagger.hilt.android.lifecycle.HiltViewModel
import heven.holt.wan.android.extensions.requestCoroutine
import heven.holt.wan.android.model.UserCoinInfo
import heven.holt.wan.android.repository.WanUserRepository
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class UserCoinViewModel @Inject constructor(
    private val wanUserRepository: WanUserRepository
) : ViewModel() {
    val userCoinInfoLiveData: MutableLiveData<UserCoinInfo> = MutableLiveData()

    val flow = wanUserRepository.getUserCoinRecordListFlow().cachedIn(viewModelScope)

    fun getUserCoinInfo() {
        viewModelScope.launch {
            val userCoinInfo: UserCoinInfo =
                requestCoroutine { wanUserRepository.getUserCoinInfo() } ?: return@launch
            userCoinInfoLiveData.postValue(userCoinInfo)
        }
    }
}